//
//  ViewFa.swift
//  CosClub
//
//  Created by 沙睿 on 2020/3/16.
//  Copyright © 2020 ios. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

// MARK: - 输入View工厂
public class InputView : UIView {
    
    public let input = UITextField(text: "", placeholder: "", fontSize: 15)
    public convenience init(title: String, value: String? = nil)
    {
        self.init()
        let titleLabel = UILabel(title, color: UIColor.app.c3a, fontSize: 16)
        
        addSubview(titleLabel)
        
        titleLabel.snp.makeConstraints { (maker) in
            maker.leading.equalToSuperview().inset(UISpecification.marginHor.rawValue)
            maker.top.bottom.equalToSuperview()
            maker.width.greaterThanOrEqualTo(64).priority(.high)
        }
        
        input.text = value
        input.placeholder = "请输入\(title)"
        addSubview(input)
        
        input.snp.makeConstraints { (maker) in
            maker.left.equalTo(titleLabel.snp.right).offset(20)
            maker.top.bottom.equalToSuperview()
            maker.trailing.equalToSuperview().inset(UISpecification.marginHor.rawValue)
            maker.width.equalToSuperview().priority(.low)
        }
        
        
        let lineView = UIView(type: .line)
        addSubview(lineView)
        
        lineView.snp.makeConstraints { (maker) in
            maker.leading.equalToSuperview().inset(UISpecification.marginHor.rawValue)
            maker.bottom.trailing.equalToSuperview()
            maker.height.equalTo(0.5)
        }
    }
}




// MARK: CELL工厂

// MARK:  默认cell模型
public struct DefauleCellModel {
    public typealias CellType = DefaultItemCell.ItemType
    
    public var type: CellType
    public var title: String?
    public var desc: String?
    public var items: [String]?
    
    public let selectedItemSubject = PublishSubject<IndexPath>()
    public var cellHeight : CGFloat {
        switch self.type {
        case .nomal:
            return UISpecification.itemHeight.rawValue
        case .tag:
            let tagHeight = TagLabelsView.calculateItemsFrame(tagsData: items, space: 8, type: .m_black, contentWidth: kScreenWidth - margin * 2, newLine: true)?.last?.maxY ?? 0
            
            if tagHeight == 0 {
                return UISpecification.itemHeight.rawValue
            }
            return  tagHeight + UISpecification.itemHeight.rawValue + 16
        }
    }
    public init(type: CellType,title: String? = nil, desc: String? = nil, items: [String]? = nil) {
        self.type = type
        self.title = title
        self.desc = desc
        self.items = items
    }
}

// MARK:  默认cell
public class DefaultItemCell: UITableViewCell {
    
    public enum ItemType : String {
        case nomal = "DefaultItemIdentifier_Nomal"
        case tag = "DefaultItemIdentifier_Tag"
    }
    public var title : String?
    public var descText : String?
    public var bag = DisposeBag()
    
    public var tagList: [String]? {
        didSet {
            creatTagView()
        }
    }
    
    public let titleLabel = UILabel("",fontSize:15)
    public let descTextLabel = UILabel("", color: UIColor.app.c3b, fontSize: 14)
    public let tagLabelsView = TagLabelsView(space: 8, type: .m_black, newLine: true)
    
    private var cellType :ItemType?
    
    public override func prepareForReuse() {
        bag = DisposeBag()
    }
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    public convenience init(title: String?, descText: String?, type: ItemType = .nomal) {
        self.init(style: .default, reuseIdentifier: type.rawValue)
        self.cellType = type
        self.backgroundColor = UIColor.white
        self.title = title
        self.descText = descText
        creatItem(title: title, desc: descText)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func creatTagView() {
        
        guard self.cellType == .tag else {
            return
        }
        
        for view in self.subviews {
            if view.restorationIdentifier == "标签" {
                view.removeFromSuperview()
            }
        }
        
        guard let list = self.tagList else {
            return
        }
        
        
        let margin = UISpecification.marginHor.rawValue
        
        
        tagLabelsView.tagList = list
        tagLabelsView.reload()
        addSubview(tagLabelsView)
        
        tagLabelsView.frame = CGRect(x:margin , y: 47, width: kScreenWidth - margin * 2, height: 20)
        
        if self.cellType == .tag && self.tagList?.count ?? 0 > 0 {
            descTextLabel.text = ""
        }
    }
    
    public func creatItem(title: String?, desc: String?){
        
        titleLabel.text = title
        descTextLabel.text = desc
        
        self.addSubview(titleLabel)
        self.addSubview(descTextLabel)
        titleLabel.snp.makeConstraints { (maker) in
            maker.leading.top.equalToSuperview().inset(16)
            maker.width.greaterThanOrEqualTo(0)
            maker.height.equalTo(19)
        }
        
        
        descTextLabel.snp.makeConstraints { (maker) in
            maker.top.equalToSuperview().inset(16)
            maker.height.equalTo(19)
            maker.trailing.equalToSuperview().inset(34)
            maker.width.greaterThanOrEqualTo(0)
        }
        
        let icon = UIImageView.init(image: UIImage.imageFromBasis(named: "cm_arrow_right"))
        self.addSubview(icon)
        icon.snp.makeConstraints { (maker) in
            maker.size.equalTo(CGSize(width: 8, height: 12))
            maker.centerY.equalTo(descTextLabel)
            maker.trailing.equalToSuperview().inset(15)
        }
        
        let lineView = UIView(type: .line)
        self.addSubview(lineView)
        lineView.snp.makeConstraints { (maker) in
            maker.leading.equalToSuperview().inset(UISpecification.marginHor.rawValue)
            maker.bottom.trailing.equalToSuperview()
            maker.height.equalTo(0.5)
        }
        
    }
}


